package cn.iocoder.yudao.module.service.service.assistantgroup;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.service.controller.admin.assistantgroup.vo.*;
import cn.iocoder.yudao.module.service.dal.dataobject.assistantgroup.ServiceAssistantGroupDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.service.dal.mysql.assistantgroup.ServiceAssistantGroupMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.service.enums.ErrorCodeConstants.*;

/**
 * 客服分组 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class ServiceAssistantGroupServiceImpl implements ServiceAssistantGroupService {

    @Resource
    private ServiceAssistantGroupMapper assistantGroupMapper;

    @Override
    public Long createAssistantGroup(ServiceAssistantGroupSaveReqVO createReqVO) {
        // 插入
        ServiceAssistantGroupDO assistantGroup = BeanUtils.toBean(createReqVO, ServiceAssistantGroupDO.class);
        assistantGroupMapper.insert(assistantGroup);
        // 返回
        return assistantGroup.getId();
    }

    @Override
    public void updateAssistantGroup(ServiceAssistantGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateAssistantGroupExists(updateReqVO.getId());
        // 更新
        ServiceAssistantGroupDO updateObj = BeanUtils.toBean(updateReqVO, ServiceAssistantGroupDO.class);
        assistantGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteAssistantGroup(Long id) {
        // 校验存在
        validateAssistantGroupExists(id);
        // 删除
        assistantGroupMapper.deleteById(id);
    }

    private void validateAssistantGroupExists(Long id) {
        if (assistantGroupMapper.selectById(id) == null) {
            throw exception(ASSISTANT_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public ServiceAssistantGroupDO getAssistantGroup(Long id) {
        return assistantGroupMapper.selectById(id);
    }

    @Override
    public PageResult<ServiceAssistantGroupDO> getAssistantGroupPage(ServiceAssistantGroupPageReqVO pageReqVO) {
        return assistantGroupMapper.selectPage(pageReqVO);
    }

}